<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>签到列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="queryinfo" class="demo-form-inline" size="mini">
        <el-form-item >
          <el-input v-model="queryinfo.title" placeholder="公司账号" clearable/>
        </el-form-item>
        <el-form-item >
          <el-input v-model="queryinfo.username" placeholder="账号账号" clearable/>
        </el-form-item>
        <el-form-item label="时间选择">
          <el-date-picker
              v-model="timer"
              type="datetimerange"
              range-separator="-"
              start-placeholder="Start date"
              end-placeholder="End date"
              :default-time="defaultTime"
              @change="changeData"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="GetData">查询</el-button>
        </el-form-item>
      </el-form>
      <hr>
      <!--数据-->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="activity_id" label="活动ID"  />
        <el-table-column prop="activity_name" label="活动名称"  />
        <el-table-column prop="username" label="用户名称"  />
        <el-table-column prop="bean" label="签到奖励"  />
        <el-table-column prop="addtime" label="签到时间" >
          <template #default="props">{{ formatDate(props.row.addtime) }}</template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
          background
          :current-page="queryinfo.page"
          :page-sizes="[3,10, 50, 100, 1000]"
          :page-size="queryinfo.size"
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
      />
    </el-card>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import {ElMessage} from "element-plus";
import axios from "@/utils/axios";
import {formatDate} from '@/utils/date'

export default {
  name: "user",
  setup() {
    const state = reactive({
      queryinfo: {
        page: 1,
        size: 10
      },
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      total: 0,
      tableData: [],

      //时间
      timer: [],
      defaultTime: [
        new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),
      ],
    })
    onMounted(() => {
      GetData()
    })


    //时间操作
    const changeData = async (row) => {
      if (row !== null) {
        state.queryinfo.startTime = ((new Date(row[0])).getTime()) / 1000
        state.queryinfo.endTime = ((new Date(row[1])).getTime()) / 1000
      } else {
        delete state.queryinfo.startTime
        delete state.queryinfo.endTime
      }
    }


    // 获取列表
    const GetData = async () => {
      const {data: res} = await axios.get('sign/record', {params: state.queryinfo})
      if (res.code !== 1000) return ElMessage.error(res.msg)
      state.tableData = res.data.list
      state.total = res.data.total
    }
    const changePage = (val) => {
      state.queryinfo.page = val
      GetData()
    }
    const handleSizeChange = (val) => {
      state.queryinfo.size = val
      GetData()
    }

    return {
      ...toRefs(state),
      GetData,
      formatDate,
      changeData,
      changePage,
      handleSizeChange,
    }
  }
}
</script>

<style scoped>

</style>